package com.example.personalblog.service.impl;

import com.example.personalblog.entity.Comment;
import com.example.personalblog.entity.Users;
import com.example.personalblog.mapper.CommentMapper;
import com.example.personalblog.repository.CommentRepository;
import com.example.personalblog.repository.UsersRepository;
import com.example.personalblog.service.CommentService;
import com.example.personalblog.vo.ShowComment;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import net.bytebuddy.asm.Advice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    CommentRepository commentRepository;

    @Autowired
    CommentMapper commentMapper;

    @Autowired
    UsersRepository usersRepository;

    @Override
    public void updateState(int id,int state) {
        commentRepository.updateState(id, state);
    }

    @Override
    public PageInfo<Comment> findAllComment(int pageNum, String str) {
        PageHelper.startPage(pageNum, 10);
        return new PageInfo<>(commentRepository.findByContentContaining(str));
    }

    @Override
    public List<Comment> findByAid(int aid) {
        return commentRepository.findByAid(aid);
    }

    @Override
    public List<ShowComment> findCommentByBlogId(int id) {
        List<ShowComment> commentList = commentMapper.findCommentByBlogId(id);
        //查询二级评论
        Iterator<ShowComment> iterator = commentList.iterator();
        while (iterator.hasNext()){
            ShowComment next = iterator.next();
            List<ShowComment> comments = new ArrayList<>();
            List<ShowComment> commentByCommentId = findCommentByCommentId(comments, next.getCid());

            //查询被评论用户
            Iterator<ShowComment> iterator1 = commentByCommentId.iterator();
            while (iterator1.hasNext()){
                ShowComment next1 = iterator1.next();
                Users user = usersRepository.findById(next1.getUser().getId());
                next1.setComUser(user);
            }

            next.setComments(commentByCommentId);
        }

        return commentList;
    }

    @Override
    public void save(Comment comment) {
        commentRepository.save(comment);
    }

    //查询二级以后的评论
    private List<ShowComment> findCommentByCommentId(List<ShowComment> comments, int id){
        List<ShowComment> commentList = commentMapper.findCommentByCommentId(id);

        Iterator<ShowComment> iterator = commentList.iterator();
        while (iterator.hasNext()){
            ShowComment next = iterator.next();
            List<ShowComment> commentByCommentId = findCommentByCommentId(comments, next.getCid());
            comments.addAll(commentByCommentId);

            //查询被评论用户
            Iterator<ShowComment> iterator1 = commentByCommentId.iterator();
            while (iterator1.hasNext()){
                ShowComment next1 = iterator1.next();
                Users user = usersRepository.findById(next1.getUser().getId());
                next1.setComUser(user);
            }
        }
        commentList.addAll(comments);
        return commentList;
    }
}
